<template>
  <div>
    <div class="login">
      <h1>登录</h1>
      <!-- form表单 -->
      <!-- rules属性:表单校验规则 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-row :gutter="10">
            <el-col :span="15">
              <el-input v-model="ruleForm.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="url" alt="" style="width: 100%; height: 40px;" @click="getCaptchaImage()">
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login, captchaImage } from '@/api/login'
export default {
  data() {
    return {
      url: '',
      ruleForm: {
        username: '',
        password: '',
        code: '',
        uuid: ''
      },
      rules: {
        username: [
          // tigger 什么时候校研表单 blur失去焦点 change 数据改变的时候
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请选择密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'change' }
        ]
      }
    };
  },
  created() {
    this.getCaptchaImage()
  },
  methods: {
    getCaptchaImage() {
      captchaImage().then(res => {
        console.log(res);
        this.url = 'data:image/gif;base64,' + res.img
        this.ruleForm.uuid = res.uuid
      })
    },
    submitForm(formName) {
      // validata 将form 校验一遍, valid参数返回true/false 表示是否通过校验
      this.$refs[formName].validate(async (valid) => {
        //如果通过校验
        if (valid) {
          // 调接口-----登录跳转
          // login(this.ruleForm).then(res => {
          //   console.log(res);
          //   if (res.code == 200) {
          //     this.$message({
          //       message: '登录成功',
          //       type: 'success'
          //     });
          //     this.$router.push({
          //       path: '/home'
          //     })
          //   }
          // })
          // .catch((err) => {

          // })
          // 调用vuex中封装好的登录方法 并传参
          await this.$store.dispatch('goLogin', this.ruleForm)
          // this.$store.dispatch('getUserInfo')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>

<style lang="scss" scoped>
.login {
  width: 500px;
  height: 300px;
  box-shadow: 1px 1px 8px rgb(0, 0, 0, 0.3);
  border-radius: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
}

h1 {
  padding: 10px 0 20px;
}
</style>